package com.nyist.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.nyist.myhandler.CustomerBlockHandler;
import com.nyist.pojo.User;
import com.nyist.result.CommonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {

    @GetMapping(value = "/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult byResource(){
        return new CommonResult("200","按资源名称限流测试OK",new User(11,"【blockHandler】按资源名称限流测试OK",11));
    }
    public CommonResult handleException(BlockException e){
        return new CommonResult("444","服务不可用"+e.getClass().getCanonicalName(),new User(444,"444",444));
    }


    @GetMapping(value = "/rateLimit/byUrl")
    @SentinelResource(value = "byUrl",blockHandler = "handleException")
    public CommonResult byUrl(){
        return new CommonResult("200","按URL限流测试OK",new User(22,"【blockHandler】按URL限流测试OK",22));
    }


    //======= customerBlockHandler

    @GetMapping(value = "/rateLimit/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
                    blockHandlerClass = CustomerBlockHandler.class,
                    blockHandler = "handlerException2")
    public CommonResult customerBlockHandler(){
        return new CommonResult("200","按用户自定义",new User(33,"【blockHandler】按用户自定义",33));
    }

}
